package com.yihuan.controller;

import com.yihuan.entity.ShoppingEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/24 16:08
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class ShoppingController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    ShoppingService shoppingService;
    
    /**
     * 废品分页查询数据
     *
     * @param session
     * @return
     */
    @RequestMapping("/findShoppingByShoppingList")
    public Map<String, Object> findShoppingByShoppingList(@RequestBody ShoppingEntity model, HttpSession session) {
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        session.setAttribute("CompID_1", model.getCompID());
        List<ShoppingEntity> findShoppingByShoppingList = shoppingService.findShoppingByShoppingList(model);
        if (findShoppingByShoppingList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SHOPPING_LIMIT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("shopping_List", findShoppingByShoppingList);
        map.put("explain ", MessageConstant.SHOPPING_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 公司分页查询数据
     *
     * @param session
     * @return
     */
    @RequestMapping("/findShoppingByShoppingCompanyList")
    public Map<String, Object> findShoppingByShoppingCompanyList(HttpSession session) {
        // model.setUserID((Integer) session.getAttribute("UserID_4"));
        ShoppingEntity model = new ShoppingEntity();
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        List<ShoppingEntity> findShoppingByShoppingList = shoppingService.findShoppingByShoppingCompanyList(model);
        if (findShoppingByShoppingList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SHOPPING_LIMIT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("shopping_List", findShoppingByShoppingList);
        map.put("explain ", MessageConstant.SHOPPING_LIMIT_SUCCESS);
        return map;
    }
    
    
    /**
     * 新增数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertShoppingByShoppingData")
    public Map<String, Object> insertShoppingByShoppingData(@RequestBody ShoppingEntity model, HttpSession session) {
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setShoppingDate(sf.format(d));
        //状态
        model.setShoppingState(1);
        int insertShoppingByShoppingData = shoppingService.insertShoppingByShoppingData(model);
        if (insertShoppingByShoppingData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SHOPPING_INSERT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.SHOPPING_INSERT_SUCCESS);
        return map;
    }
    
    /**
     * 删除数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateShoppingByShoppingDeleteState")
    public Map<String, Object> updateShoppingByShoppingDeleteState(@RequestBody ShoppingEntity model, HttpSession session) {
        model.setShoppingState(2);
        int updateShoppingByShoppingDeleteState = shoppingService.updateShoppingByShoppingDeleteState(model);
        if (updateShoppingByShoppingDeleteState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.SHOPPING_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.SHOPPING_UPDATE_SUCCESS);
        return map;
    }
    
}
